package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.ehs.dto.check.CheckItemDto;
import qc.module.ehs.entity.EhsCheckItem;

import java.util.List;

/**
 * EhsCheckItemMapper
 *
 * @author QuCheng Tech
 * @create 2024/10/23
 */
@Mapper
public interface EhsCheckItemMapper {
    EhsCheckItemMapper MAPPER = Mappers.getMapper(EhsCheckItemMapper.class);

    /**
     * entity -> dto
     * @param en
     * @return
     */
    @Mapping(target = "status", source = "status.index")
    CheckItemDto toDto(EhsCheckItem en);

    /**
     * List<entity> -> List<dto>
     * @param ens
     * @return
     */
    List<CheckItemDto> toDtoList(List<EhsCheckItem> ens);

    /**
     * dto -> entity
     * @param dto
     * @return
     */
    @Mapping(target = "status", ignore = true)
    EhsCheckItem toEntity(CheckItemDto dto);

    default EhsCheckItem dtoToEntity(CheckItemDto dto) {
        EhsCheckItem en = EhsCheckItemMapper.MAPPER.toEntity(dto);

        en.setStatus(getResourceStatusFlagEnum(dto.getStatus()));

        return en;
    }

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认状态为0--正常
        return ResourceStatusFlagEnum.NORMAL;
    }
}
